package com.firstgroup.service.Impl;

import com.firstgroup.Utils.ResponseResult;
import com.firstgroup.config.COSconfig;
import com.firstgroup.domain.Interviewers;
import com.firstgroup.repository.InterviewersMapper;
import com.firstgroup.repository.InterviewersRepository;
import com.firstgroup.service.InterViewService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 创作人 王箫凯
 * Date 2021/8/10
 * Time 16:19
 *
 * @author 王箫凯
 */
@Service
@Slf4j
@Transactional
public class InterViewServiceImpl implements InterViewService {
    @Resource
    COSconfig coSconfig;
    @Resource
    COSClient cosClient;
    @Autowired
    InterviewersRepository interviewersRepository;
    @Resource
    InterviewersMapper interviewersMapper;

    @Override
    public ResponseResult upLoad(MultipartFile file, Integer interviewId,String path) {
        //到达此处的文件都是符合标准的文件
        int count = 0;
        InputStream is;
        String pic;
        String picName;
        String picType = "";
            try {
                is = new BufferedInputStream(file.getInputStream());
                pic = UUID.randomUUID().toString().replace("-", "");
                picName = file.getOriginalFilename();
                //进行截取
                assert picName != null;
                //路径加名称+后缀
                picType = coSconfig.getPrefix() + pic + picName.substring(picName.lastIndexOf("."));
                ObjectMetadata objectMetadata = new ObjectMetadata();
                //设置流的大小
                objectMetadata.setContentLength(file.getSize());
                //进行上传
                PutObjectRequest putObjectRequest = new PutObjectRequest(coSconfig.getBucketName(), picType, is, objectMetadata);
                PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
                if (putObjectResult.getETag() != null) {
                    //说明上传成功
                    picType=coSconfig.getPath()+"/"+picType;
                    if ("0".equals(path)){
                        //正面上传
                        count+=interviewersRepository.updateFirst(picType,interviewId);
                    }
                    if ("1".equals(path)){
                        count+=interviewersRepository.updateNext(picType,interviewId);
                    }
                }
            } catch (Exception e) {
                System.out.println(e.getCause());
                log.info("发生异常");
            }
            if (count>0){
                return ResponseResult.success("上传成功",picType);
            }

        return ResponseResult.error("上传失败");
    }

    @Override
    public Interviewers findByInterviewersId(int interviewerId) {
        return interviewersRepository.findByInterviewersId(interviewerId);
    }

    @Override
    public Integer deleteById(int id) {
        interviewersRepository.deleteById(id);

        return 1;
    }

    @Override
    @Transactional
    @Rollback(value = false)
    public int batchDeleteInterviewers(Integer[] id) {
        int i =0;
        for (Integer integer : id) {
            interviewersRepository.deleteById(integer);
            i++;
        }
        return i;
    }

    @Override
    public List<Interviewers> findInterviewersByState(String state) {
        if(state.equals("所有人")){
            return interviewersRepository.findAll();
        }else{
            return interviewersRepository.findInterviewersByState(state);
        }
    }

    @Override
    public List<Interviewers> findByNameLike(String name) {
        return interviewersRepository.findByNameLike(name);
    }

    @Override
    public ResponseResult updateInterviews(Integer id,String name, String qq, String studentId, String classes, String telephone, String sex) {
        //先进行查找操作检查该对象是否存在
        Interviewers interviewers = interviewersRepository.findInterviewersByInterviewersId(id);

        //Interviewers interviewers1=interviewersRepository.findByStudentId(studentId);

        if (interviewers == null) {
            return ResponseResult.error("不存在该信息");
        }
        interviewers.setClasses(classes);
        interviewers.setName(name);
        interviewers.setQq(qq);
        interviewers.setSex(sex);
        interviewers.setStudentId(studentId);
        interviewers.setTelephone(telephone);
        int i = interviewersRepository.updateInterviews(name, qq, studentId, classes, telephone, sex, id);
        if (i>0){
            return ResponseResult.success("修改成功", interviewers);
        }

        //if(interviewers1==null){//如果学号为空就直接修改
        //  return ResponseResult.success("修改成功",interviewersRepository.updateInterviews(name, qq, studentId, classes, telephone, sex,id));
        //}else if(interviewers1.getInterviewersId().equals(id)){//如果修改的学号等于当前登录账号的学号就直接修改
        //    int count=  interviewersRepository.updateInterviews(name, qq, studentId, classes, telephone, sex,id);
        //    System.out.println(count);
        //    interviewers.setClasses(classes);
        //    interviewers.setName(name);
        //    interviewers.setQq(qq);
        //    interviewers.setSex(sex);
        //    interviewers.setStudentId(studentId);
        //    interviewers.setTelephone(telephone);
        //    return ResponseResult.success("更新成功",interviewers);
        //}else if(interviewersRepository.findInterviewersByStudentId(studentId)!=null){//如果输入的学号和前两个不相等并且该学号存在,就会报已经存在
        //        log.info("学号{}已经被占用",studentId);
        //        return ResponseResult.error("该学号已经存在了");
        //}
        //到达此处为正确的信息
        return ResponseResult.error("未知错误");
    }

    @Override
    public int updateTimeAndPlace(String state, String place, String time) {
        return interviewersRepository.updateTimeAndPlace(state, place, time);
    }


    @Override
    public int saveUsers(String user,int id) {
        return interviewersRepository.saveUsers(user,id);
    }

    @Override
    public List<Interviewers> findByUserLike(String user) {
        return interviewersRepository.findByUserLike(user);
    }

    @Override
    public Interviewers addInterviewers(Interviewers interviewers) {
        return interviewersRepository.save(interviewers);
    }

    @Override
    public ResponseResult findByYear(String year) {
        List<Interviewers> interviewersList=interviewersRepository.findByYear(year);
        return ResponseResult.success("查询成功",interviewersList);
    }

    @Override
    public ResponseResult findRepeatResister() {
        List<Interviewers> interviewersList=interviewersRepository.findStudentIdByRepeat();
        List<Integer> integers= interviewersList.stream().map(Interviewers::getInterviewersId).collect(Collectors.toList());
        ArrayList<Interviewers> arrayList=new ArrayList<>();
        String num="";
        for(int x:integers){//1,1,1,2,2,3,3
            Interviewers interviewers=interviewersRepository.findByInterviewersId(x);
            if(!num.equals(interviewers.getStudentId())){
                arrayList.add(interviewers);
            }
            num=interviewers.getStudentId();
        }
        return ResponseResult.success("查询成功",arrayList);
    }

    @Override
    public ResponseResult findInterviewersByStudentId(String studentId) {
        List<Interviewers> interviewers=interviewersRepository.findAllInterviewersByStudentId(studentId);
        return ResponseResult.success("查询成功",interviewers);
    }

}
